package com.huarui.purchaseOrder.service;

import java.util.Map;

import com.huarui.purchaseOrder.enums.PurchaseFunctionEnum;
import com.huarui.purchaseOrder.enums.PurchaseOperateMesEnum;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class PurchaseOrderServiceUtil {
	public static final String ALERTMES = "pur_alert_mes";
	
	public static final String ORDERSID = "ordersId";
	
	public static final String FUNC = "loadedMesesKeys";
	
	public static final String STATUS = "status";
	
	public static final String REMOVE = "remove";
	
	public static final String REMOVE_YES = "1";
	
	public static final void toShowOrders(Map<String, Object> mesMap, PurOrSaleOrder orders) {
		if(orders == null) {
			mesMap.put(ALERTMES, PurchaseOperateMesEnum.ORDERS_NOT_EXIST.getKey());
			mesMap.put(REMOVE, REMOVE_YES);
			mesMap.put(FUNC, PurchaseFunctionEnum.CALLBACK_ALERT_FUNC.getKey());
		} else {
//			mesMap.put(ALERTMES, PurchaseOperateMesEnum.ORDERS_OK.getKey());
			mesMap.put(ORDERSID, orders.getId());
			mesMap.put(STATUS, getStatus(orders));
			mesMap.put(FUNC, PurchaseFunctionEnum.LOAD_PUR_FORM.getKey() + ":" + 
							PurchaseFunctionEnum.lOAD_DETAILS_GRID.getKey() + ":" + 
							PurchaseFunctionEnum.LOAD_BUTTONS_TOOL_BAR.getKey());
		}
		
	}
	
	private static final String getStatus(PurOrSaleOrder orders) {
		SessionMessage se = UserHolder.getCurrentSessionMessage();
		Integer userId = se.getUserId();
		if(userId == null) {
			throw new RuntimeException();
		}
		if(PurOrSaleOrder.STATUS_TMP.equals(orders.getStatus())) {
			if(userId.equals(orders.getCurrentorId())) {
				return orders.getStatus();
			} else {
				return orders.getStatus() + "-";
			}
		} else {
			return orders.getStatus();
		}
	}
}
